
using System;
using System.IO;
using System.Text;
using QdbUnit;
using QdbUnit.Extensions.Util;

namespace Qdb.Tests.Util
{
	class JavaServices
	{
		public static bool CanRunJavaCompatibilityTests()
		{
#if CF 
			return false;
#else
			if (null == WorkspaceServices.WorkspaceRoot)
			{
				Console.WriteLine("'qdbbuild' directory not found, skipping java compatibility test.");
				return false;
			}
			return true;
#endif
		}

        public static string QdbCoreJarPath()
        {
            return QdbJarPath("-core");
        }

		public static string QdbJarPath(string extension)
		{
			string qdbVersion = string.Format("{0}.{1}.{2}.{3}", QdbVersion.Major, QdbVersion.Minor,
                QdbVersion.Iteration, QdbVersion.Revision);
			string distDir = WorkspaceServices.ReadProperty(WorkspaceServices.MachinePropertiesPath(), "dir.dist", true);
			if(string.IsNullOrEmpty(distDir))
			{
				distDir = "qdbbuild/dist";
			}
			return WorkspaceServices.WorkspacePath(distDir + "/java/lib/qdb-" + qdbVersion + extension + "-java1.2.jar");
		}

		public static string JavaTempPath
		{
			get { return IOServices.BuildTempPath("java"); }
		}

		public static void ResetJavaTempPath()
		{
			string tempPath = JavaServices.JavaTempPath;
			if (Directory.Exists(tempPath)) Directory.Delete(tempPath, true);
		}

		public static string CompileJavaCode(string fname, string code)
		{
			string srcFile = Path.Combine(JavaServices.JavaTempPath, fname);
			IOServices.WriteFile(srcFile, code);
			return javac(srcFile);
		}

		public static string javac(string srcFile)
		{
#if CF || SILVERLIGHT 
            return null;
#else
            Assert.IsTrue(File.Exists(JavaServices.QdbCoreJarPath()), string.Format("'{0}' not found. Make sure the jar was built before running this test.", JavaServices.QdbCoreJarPath()));
			Assert.IsTrue(File.Exists(JavaServices.QdbJarPath("-optional")), string.Format("'{0}' not found. Make sure the jar was built before running this test.", JavaServices.QdbJarPath("-optional")));
			Assert.IsTrue(File.Exists(JavaServices.QdbJarPath("-cs")), string.Format("'{0}' not found. Make sure the jar was built before running this test.", JavaServices.QdbJarPath("-cs")));
			return IOServices.Exec(WorkspaceServices.JavacPath(),
                    "-classpath",
                    IOServices.JoinQuotedArgs(
									Path.PathSeparator, 
									QdbCoreJarPath(), 
									QdbJarPath("-optional"),
									QdbJarPath("-cs")),
                    srcFile);
#endif
		}

		public static string java(string className, params string[] args)
		{
#if CF || SILVERLIGHT 
            return null;
#else
            return IOServices.Exec(WorkspaceServices.JavaPath(),
                    "-cp",
                    IOServices.JoinQuotedArgs(
									Path.PathSeparator, 
									JavaServices.JavaTempPath, 
									QdbCoreJarPath(), 
									QdbJarPath("-optional"),
									QdbJarPath("-cs")),
                    className,
                    IOServices.JoinQuotedArgs(args));
#endif
        }
	}
}
